<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\user\UserBeanLogModel;
use app\model\user\UserEarningsModel;
use app\model\user\UserModel;
use think\Request;

/**
 * DepositBean
 * Class DepositBean
 * @package app\controller
 */
class DepositBean extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new UserBeanLogModel();
    }

    /**
     * 分页列表
     * @param string $check
     * @param string $keyword
     * @param int $page
     * @param int $limit
     * @return \think\response\Json
     */
    public function getPage($check = '', $keyword = '', $page = 1, $limit = 10)
    {
        $earnings_model = $this->model;
        $earnings_model = $earnings_model->alias('a')
            ->join("t_user b", "a.user_id=b.id")
            ->Keyword($keyword)
            ->where('a.type', 'deposit');
        if (intval($check) !== -1) {
            $earnings_model = $earnings_model->where("a.check", $check);
        }
        $dataList = $earnings_model
            ->field("a.id,a.deposit,a.add_date,a.check,a.status,b.nick_name,b.head_img,b.phone")
            ->order('id DESC')
            ->paginate(['page' => $page, 'list_rows' => $limit], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $items = $dataList->items();
        return self::returnMsg(1, [
            'totalCount' => $total,
            'currentPage' => $current,
            'pageList' => $items
        ]);
    }


    /**
     *审核提现
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function onStatus(Request $request)
    {
        $formData = $request->param();
        $id = $formData['id'];
        $detail = isset($formData['detail']) ? $formData['detail'] : "";
        $check = $formData['check'];
        $up_date = [
            'detail' => $detail,
            'check' => $check
        ];
        $ret = $this->model->where('id', $id)->update($up_date);
        $out_data = [
            'code' => 1,
            'msg' => "操作成功"
        ];
        if ($ret != false) {
            if (intval($check) == 2) {
                $user_model = new  UserModel();
                $ret = $user_model->payUserDeposit('bean', $id);
                if ($ret['code'] == 1) {
                    return self::returnMsg($out_data);
                }
                $out_data = [
                    'code' => 0,
                    'msg' => "打款失败"
                ];
                return self::returnMsg($out_data);
            }
            return self::returnMsg($out_data);
        }
        $out_data = [
            'code' => 0,
            'msg' => "操作失败"
        ];
        return self::returnMsg($out_data);
    }

    /**
     * 获取提现详情
     * @param $id
     * @return \think\response\Json
     */
    public function onInfo($id)
    {
        $data = $this->model->alias('a')
            ->join("t_user b", "a.user_id=b.id")
            ->field("a.*,b.nick_name,b.head_img,b.phone")
            ->where('a.id', $id)->find();
        if (!empty($data)) {
            return self::returnMsg(1, $data);
        } else {
            return self::returnMsg(1001);
        }
    }
}
